package com.sgdh.project.terminal.terminal.controller;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sgdh.framework.aspectj.lang.annotation.Log;
import com.sgdh.framework.aspectj.lang.enums.BusinessType;
import com.sgdh.framework.web.controller.BaseController;
import com.sgdh.framework.web.domain.AjaxResult;
import com.sgdh.framework.web.page.TableDataInfo;
import com.sgdh.project.terminal.program.domain.Program;
import com.sgdh.project.terminal.program.service.IProgramService;
import com.sgdh.project.terminal.terminal.domain.Terminal;
import com.sgdh.project.terminal.terminal.service.ITerminalService;

/**
 * 终端信息管理
 * 
 * @author px
 */
@Controller
@RequestMapping("/sks/terminal")
public class TerminalController extends BaseController
{
    private String prefix = "sks/terminal";

    @Autowired
    private ITerminalService terminalService;
    
    @Autowired
    private IProgramService programService;


    @RequiresPermissions("sks:terminal:view")
    @GetMapping()
    public String terminal()
    {
        return prefix + "/terminal";
    }

    @RequiresPermissions("sks:terminal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Terminal terminal)
    {
        startPage();
        List<Terminal> list = terminalService.selectTerminalList(terminal);
        return getDataTable(list);
    }


    /**
     * 新增终端
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	Program program = new Program();
    	mmap.put("programList", programService.selectProgramList(program));
        return prefix + "/add";
    }
    
    @GetMapping("/selectProgramList")
    @ResponseBody
    public AjaxResult selectProgramList()
    {
    	Program program = new Program();
        return AjaxResult.success(programService.selectProgramList(program));
    }
    

    
    /**
     * 新增保存终端
     */
    @RequiresPermissions("sks:terminal:add")
    @Log(title = "终端管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Terminal terminal)
    {
        return toAjax(terminalService.insertTerminal(terminal));
    }

    /**
     * 修改终端
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap)
    {
        mmap.put("terminal", terminalService.selectTerminalById(id));
        return prefix + "/edit";
    }
    
    /**
     * 远程终端
     */
    @RequiresPermissions("sks:terminal:rangFlag")
    @GetMapping("/rangTerminal")
    @ResponseBody
    public AjaxResult rangTerminal(int id)
    {
        return toAjax(terminalService.rangTerminal(id));
    }

    /**
     * 修改保存终端
     */
    @RequiresPermissions("sks:terminal:edit")
    @Log(title = "终端管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Terminal terminal)
    {
        return toAjax(terminalService.updateTerminal(terminal));
    }


    @RequiresPermissions("sks:terminal:remove")
    @Log(title = "终端管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(terminalService.deleteTerminalByIds(ids));
    }

}